package cn.mw.cmdb.template.strategy.handler;

import cn.mw.cmdb.exception.ZabbixResultException;
import cn.mw.cmdb.template.constant.MwTemplateConfigurationConstant;
import cn.mw.cmdb.template.entity.convert.MwValueMapConvertDto;
import cn.mw.cmdb.template.enums.TemplateDetailEnum;
import cn.mw.cmdb.template.param.MwTemplateConfigurationParam;
import cn.mw.cmdb.template.param.ValueMappingParam;
import cn.mw.cmdb.template.strategy.TemplateDetailHandler;
import cn.mw.cmdb.template.utils.TemplateSortUtil;
import cn.mw.microMonitorCommon.constants.ZabbixApiConstants;
import cn.mw.microMonitorCommon.constants.ZabbixMethodConstants;
import cn.mw.microMonitorCommon.entity.common.MwTPServerTable;
import cn.mw.microMonitorCommon.entity.zabbix.MWZabbixAPIResult;
import cn.mw.microMonitorCommon.util.ListMapObjUtils;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description 值映射处理
 */
@Service
@Slf4j
public class ValueMappingHandler extends TemplateDetailHandler {
    @Override
    public TemplateDetailEnum handlerType() {
        return TemplateDetailEnum.VALUE_MAPPING;
    }

    @Override
    public Object getData(MwTemplateConfigurationParam param) {
        Map zabbixValueMapParam = new HashMap();
        zabbixValueMapParam.put(MwTemplateConfigurationConstant.OUTPUT,MwTemplateConfigurationConstant.EXTEND);
        zabbixValueMapParam.put(MwTemplateConfigurationConstant.selectMappings,MwTemplateConfigurationConstant.EXTEND);
        MWZabbixAPIResult valueMapResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.VALUE_MAP_GET, zabbixValueMapParam);
        if(valueMapResult == null || valueMapResult.isFail()){
            throw new ZabbixResultException(String.valueOf(valueMapResult.getData()));
        }
        List<MwValueMapConvertDto> valueMapConvertDtos = new ArrayList<>();
        try {
            valueMapConvertDtos = JSONArray.parseArray(String.valueOf(valueMapResult.getData()), MwValueMapConvertDto.class);
        }catch (Throwable e){
            log.error("值映射转换JSON异常",e);
        }
        if(CollectionUtils.isNotEmpty(valueMapConvertDtos) && StringUtils.isNotEmpty(param.getDetailName())){
            valueMapConvertDtos = valueMapConvertDtos.stream().filter(item->item.getName().contains(param.getDetailName())).collect(Collectors.toList());
        }
        List realData = TemplateSortUtil.templateSortList(valueMapConvertDtos,param,param.getQuery());
        if(param.getPageNum() == null || param.getPageSize() == null){
            return realData;
        }
        Page page = pageHandler(realData, param.getPageNum(), param.getPageSize());
        return page;
    }

    @Override
    public Object createData(MwTemplateConfigurationParam param) {
        Object detailData = param.getDetailData();
        ValueMappingParam valueMappingParam = JSONObject.parseObject(JSONObject.toJSONString(detailData), ValueMappingParam.class);
        MwTPServerTable tpServerTable = tpServerService.getMwTPServerByServiceId(param.getServerId());
        if(tpServerTable == null){
            return null;
        }
        String monitoringServerVersion = tpServerTable.getMonitoringServerVersion();
        Map zabbixParam = null;
        switch (monitoringServerVersion){
            case ZABBIX_V5:
                zabbixParam = ListMapObjUtils.beanToMap(valueMappingParam);
                break;
            default:
                valueMappingParam.setHostid(param.getTemplateId());
                zabbixParam = ListMapObjUtils.beanToMap(valueMappingParam);
        }
        delMapNullValue(zabbixParam);
        MWZabbixAPIResult zabbixAPIResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.VALUE_MAP_CREATE, zabbixParam);
        if(zabbixAPIResult == null || zabbixAPIResult.isFail()){
            throw new ZabbixResultException(String.valueOf(zabbixAPIResult.getData()));
        }
        return null;
    }

    @Override
    public Object updateData(MwTemplateConfigurationParam param) {
        Object detailData = param.getDetailData();
        ValueMappingParam valueMappingParam = JSONObject.parseObject(JSONObject.toJSONString(detailData), ValueMappingParam.class);
        Map zabbixParam = ListMapObjUtils.beanToMap(valueMappingParam);
        delMapNullValue(zabbixParam);
        MWZabbixAPIResult zabbixAPIResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.VALUE_MAP_UPDATE, zabbixParam);
        if(zabbixAPIResult == null || zabbixAPIResult.isFail()){
            throw new ZabbixResultException(String.valueOf(zabbixAPIResult.getData()));
        }
        return null;
    }

    @Override
    public Object deleteData(MwTemplateConfigurationParam param) {
        MWZabbixAPIResult zabbixAPIResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.VALUE_MAP_DELETE, param.getDetailDeleteIds());
        if(zabbixAPIResult == null || zabbixAPIResult.isFail()){
            throw new ZabbixResultException(String.valueOf(zabbixAPIResult.getData()));
        }
        return null;
    }

    @Override
    public Object selectDataById(MwTemplateConfigurationParam param) {
        Map zabbixParam = new HashMap();
        zabbixParam.put(MwTemplateConfigurationConstant.VALUE_MAP_IDS, Arrays.asList(param.getDetailId()));
        zabbixParam.put(MwTemplateConfigurationConstant.OUTPUT,MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectMappings,MwTemplateConfigurationConstant.EXTEND);
        MWZabbixAPIResult zabbixAPIResult = zabbixDubboService.getZabbixApiResult(ZabbixApiConstants.GET_CUSTOM_INFO_BY_METHOD, param.getServerId(), ZabbixMethodConstants.VALUE_MAP_GET, zabbixParam);
        if(zabbixAPIResult == null || zabbixAPIResult.isFail()){
            throw new ZabbixResultException(String.valueOf(zabbixAPIResult.getData()));
        }
        List<ValueMappingParam> valueMappingParams = JSONArray.parseArray(String.valueOf(zabbixAPIResult.getData()), ValueMappingParam.class);
        if(CollectionUtils.isEmpty(valueMappingParams)){return null;}
        return valueMappingParams.get(0);
    }
}
